const { src, dest, series, watch } = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const autoprefixer = require('gulp-autoprefixer')
const cached = require('gulp-cached')
const fileInclude = require('gulp-file-include')
const webpack = require('webpack-stream');
const flatten = require('gulp-flatten');
const child_process = require('child_process');
const rename = require("gulp-rename");
/**@type {child_process.ChildProcess} */
let cp

function restart(cd) {
    if (cp) {
        cp.kill()
        console.log('restart');
    }
    console.log('start-up');
    cp = child_process.fork('./app/index.js')
    cp.on('error', (err) => {
        console.log('cp err', err);
    })
    if (cd) cp.on('spawn', cd)
}

function refresh(cd) {
    if (!cp) {
        return
    } else {
        cp.send('refresh')
        console.log('refresh');
        if (cd) cd()
    }
}

const development = [
    // function delFile(cd) {
    //    return del('./dist/**')
    // },
    function js() {
        return src('src/js/**/*.js')
            .pipe(cached('js'))
            .pipe(dest('./dist/js'))
    },
    function scss() {
        return src('src/scss/*.scss')
            .pipe(cached('scss'))
            .pipe(sass())
            .pipe(autoprefixer())
            .pipe(dest('./dist/css'))
    },
    function css() {
        return src('src/css/*')
            .pipe(cached('css'))
            .pipe(flatten())
            .pipe(dest('./dist/css'))
    },
    function img() {
        return src('src/img/*')
            .pipe(cached('img'))
            .pipe(dest('./dist/img'))
    },
    function upload() {
        return src('src/upload/*')
            .pipe(cached('upload'))
            .pipe(dest('./dist/upload'))
    },
    function resources() {
        return src('src/resources/*')
            .pipe(cached('resources'))
            .pipe(dest('./dist/resources'))
    },
    function html() {
        return src('src/*.html')
            .pipe(fileInclude({
                domainThrown: true
            }))
            .pipe(cached('html'))
            .pipe(dest('./dist'))
    },
    function fonts() {
        return src(['src/fonts/*', 'src/fontpack/**/*.ttf', 'src/fontpack/*/iconfont.css'])
            .pipe(cached('fonts'))
            .pipe(flatten())
            .pipe(dest('./dist/fonts'))
    },
    refresh
]

const entry = [
    () => {
        return src('src/js/index.entry.js')
            .pipe(webpack())
            .pipe(rename('index.entry.js'))
            .pipe(dest('dist/entry'))
    },
    () => {
        return src('src/js/notes.entry.js')
            .pipe(webpack())
            .pipe(rename('notes.entry.js'))
            .pipe(dest('dist/entry'))
    },
    () => {
        return src('src/js/search.entry.js')
            .pipe(webpack())
            .pipe(rename('search.entry.js'))
            .pipe(dest('dist/entry'))
    },
]

const task = {
    watch() {
        watch('./app', { ignoreInitial: false }, restart)
        watch('./src', { ignoreInitial: false }, series(...development))
    },
    entry : series(...entry)
}

module.exports = task